<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible" append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="字典类型ID" prop="dictTypeId">
      <el-input v-model="dataForm.dictTypeId" placeholder="字典类型ID"></el-input>
    </el-form-item> -->
    <el-form-item label="字典标签" prop="dictLabel">
      <el-input v-model="dataForm.dictLabel" placeholder="字典标签"></el-input>
    </el-form-item>
    <el-form-item label="字典值" prop="dictValue">
      <el-input v-model="dataForm.dictValue" placeholder="字典值"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          dictTypeId: '',
          dictLabel: '',
          dictValue: '',
          remark: '',
          sort: '',
          createTime: '',
          updateId: '',
          updateTime: '',
          deleteTag: ''
        },
        dataRule: {
              dictLabel: [
            { required: true, message: '字典标签不能为空', trigger: 'blur' }
          ],
              dictValue: [
            { required: true, message: '字典值不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id, dictTypeId) {
        console.log(dictTypeId);
        this.dataForm.id = id || 0
        this.dataForm.dictTypeId = dictTypeId
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysdictdata/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.dataForm.dictTypeId = data.data.dictTypeId
                this.dataForm.dictLabel = data.data.dictLabel
                this.dataForm.dictValue = data.data.dictValue
                this.dataForm.remark = data.data.remark
                this.dataForm.sort = data.data.sort
                this.dataForm.createTime = data.data.createTime
                this.dataForm.updateId = data.data.updateId
                this.dataForm.updateTime = data.data.updateTime
                this.dataForm.deleteTag = data.data.deleteTag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysdictdata/${!this.dataForm.id ? 'save' : 'update'}`),
              method: `${!this.dataForm.id ? 'post' : 'put'}`,
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'dictTypeId': this.dataForm.dictTypeId,
                'dictLabel': this.dataForm.dictLabel,
                'dictValue': this.dataForm.dictValue,
                'remark': this.dataForm.remark,
                'sort': this.dataForm.sort,
                'createTime': this.dataForm.createTime,
                'updateId': this.dataForm.updateId,
                'updateTime': this.dataForm.updateTime,
                'deleteTag': this.dataForm.deleteTag
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
